/**
 *  (c)
 * <p>
 *
 * <p>
 *
 */

package com.nn.smart.bus.controller;

import com.nn.smart.bus.commons.tools.annotation.LogOperation;
import com.nn.smart.bus.commons.tools.constant.Constant;
import com.nn.smart.bus.commons.tools.page.PageData;
import com.nn.smart.bus.commons.tools.utils.Result;
import com.nn.smart.bus.commons.tools.validator.AssertUtils;
import com.nn.smart.bus.commons.tools.validator.ValidatorUtils;
import com.nn.smart.bus.commons.tools.validator.group.DefaultGroup;
import com.nn.smart.bus.commons.tools.validator.group.UpdateGroup;
import com.nn.smart.bus.dto.SysDictDTO;
import com.nn.smart.bus.dto.SysSimpleDictDTO;
import com.nn.smart.bus.dto.epdc.form.SysPopulationSimpleDictFormDTO;
import com.nn.smart.bus.dto.epdc.result.EpdcAppSysDictResultDTO;
import com.nn.smart.bus.dto.epdc.result.SysPopulationSimpleDictDTO;
import com.nn.smart.bus.service.SysDictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据字典
 *
 * @author nanning
 * @since 1.0.0
 */
@RestController
@RequestMapping("dict")
@Api(tags = "数据字典")
public class SysDictController {
    @Autowired
    private SysDictService sysDictService;

    @GetMapping("page")
    @ApiOperation("字典分类")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "dictType", value = "字典类型", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "dictName", value = "字典名称", paramType = "query", dataType = "String")
    })
    public Result<PageData<SysDictDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params) {
        //字典分类
        PageData<SysDictDTO> page = sysDictService.page(params);

        return new Result<PageData<SysDictDTO>>().ok(page);
    }


    @GetMapping("sublist/{pid}")
    @ApiOperation("子列表")
    public Result<List<SysDictDTO>> sublist(@PathVariable("pid") String pid) {
        Map map = new HashMap();
        map.put("pid", pid);
        List<SysDictDTO> list = sysDictService.subList(map);
        return new Result<List<SysDictDTO>>().ok(list);
    }

    @GetMapping("list")
    @ApiOperation("字典分类数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dictName", value = "字典名称", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "dictValue", value = "字典值", paramType = "query", dataType = "String")
    })
    public Result<List<SysDictDTO>> list(@ApiIgnore @RequestParam Map<String, Object> params) {
        //字典分类数据
        List<SysDictDTO> list = sysDictService.list(params);

        return new Result<List<SysDictDTO>>().ok(list);
    }

    @GetMapping("{id}")
    @ApiOperation("信息")
    public Result<SysDictDTO> get(@PathVariable("id") Long id) {
        SysDictDTO data = sysDictService.get(id);

        return new Result<SysDictDTO>().ok(data);
    }

    @PostMapping
    @ApiOperation("保存")
    @LogOperation("Save Dict")
    public Result save(@RequestBody SysDictDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, DefaultGroup.class);

        sysDictService.save(dto);

        return new Result();
    }

    @PutMapping
    @ApiOperation("修改")
    @LogOperation("Update Dict")
    public Result update(@RequestBody SysDictDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        sysDictService.update(dto);

        return new Result();
    }

    @DeleteMapping
    @ApiOperation("删除")
    @LogOperation("Delete Dict")
    public Result delete(@RequestBody Long[] ids) {
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        sysDictService.delete(ids);

        return new Result();
    }

    /**
     * 根据数据字典类型获取简版数据字典列表，用于页面下拉菜单
     *
     * @param dictType 数据字典类型
     * @return com.nn.smart.commons.tools.utils.Result<java.util.List < com.nn.smart.dto.SysSimpleDictDTO>>
     * @author nanning
     * @date 2019/7/15 09:33
     */
    @GetMapping("listSimple/{dictType}")
    @ApiOperation("字典分类数据")
    public Result<List<SysSimpleDictDTO>> listSimpleDictInfo(@PathVariable("dictType") String dictType) {
        //字典分类数据
        return sysDictService.listSimpleDictInfo(dictType);
    }

    @DeleteMapping("{id}")
    @ApiOperation("删除")
    @LogOperation("Delete Dict")
    public Result delete(@PathVariable Long id) {
        //效验数据
        sysDictService.delete(id);
        return new Result();
    }
    /**
     * 根据数据字典类型获取详细数据字典列表
     *
     * @param dictType
     * @return com.nn.smart.bus.commons.tools.utils.Result<java.util.List<com.nn.smart.bus.dto.epdc.result.EpdcAppSysDictResultDTO>>
     * @author nanning
     * @since 2020/10/12 9:27
     */
    @GetMapping("listFull/{dictType}")
    @ApiOperation("字典分类数据")
    public Result<List<EpdcAppSysDictResultDTO>> listFullDictInfo(@PathVariable("dictType") String dictType) {
        //字典分类数据
        return sysDictService.listFullDictInfo(dictType);
    }


    @GetMapping("listPopulationSimple")
    public Result<List<SysPopulationSimpleDictDTO>> listPopulationSimple(@RequestBody SysPopulationSimpleDictFormDTO sysPopulationSimpleDictFormDTO) {
        //字典分类数据
        return sysDictService.listPopulationSimple(sysPopulationSimpleDictFormDTO);
    }

}
